package com.menxin.queueImpl;

import java.util.stream.IntStream;

/**
 * Created by Menx on 2019/3/8.
 */
public class LinkedArray<E> {

    private Node dummaryNode;
    private int size;
    private class Node{
        public E element ;
        public Node next;
        public Node(E e,Node next){
            this.element = e;
            this.next = next;
        }

        public Node(){
            this(null, null);
        }

        @Override
        public String toString(){
            return String.valueOf(element);
        }
    }

    public LinkedArray(){
        dummaryNode = new Node();
        size = 0;
    }

    public void put(E e,int index){
        if(index < 0 || index > size){
            throw new IllegalArgumentException("put failed .Illege index.");
        }
        Node prev = dummaryNode;
        for(int i =0;i < index;i ++){
            prev = prev.next;
        }
        prev.next = new Node(e,prev.next);
        size ++;
    }

    public void addLast(E e){
        put(e,size);
    }

    public void addFirst(E e){
        put(e,0);
    }

    public int getSize(){
        return size;
    }

    public boolean isEmpty(){
        return size == 0;
    }

    public boolean contains(E e){
        Node cur = dummaryNode.next;
        while(cur.next != null){
            if(cur.element.equals(e)){
                return true;
            }
        }
        return false;
    }

    public E get(int index){
        if(index < 0 || index > size){
            throw new IllegalArgumentException("get failed .Illege index.");
        }

        Node cur = dummaryNode.next;
        for(int i = 0;i < index;i ++){
            cur = cur.next;
        }

        return cur.element;
    }

    public void set(E e,int index){
        if(index < 0 || index > size){
            throw new IllegalArgumentException("set failed .Illege index.");
        }
        Node cur = dummaryNode.next;
        for(int i = 0;i < index;i ++){
            cur = cur.next;
        }
        cur.element = e;

    }

    public E remove(int index){
        if(index < 0 || index > size){
            throw new IllegalArgumentException("remove failed.Illeage index.");
        }
        Node prev = dummaryNode;
        for(int i = 0;i < index;i ++){
            prev = prev.next;
        }

        Node delNode = prev.next;
        prev.next = delNode.next;
        delNode.next = null;
        size --;
        return delNode.element;
    }

    public E removeLast(){
        return remove(size - 1);
    }

    public E removeFirst(){
        return remove(0);
    }


    @Override
    public String toString(){
        StringBuilder buf = new StringBuilder();
        buf.append("LinkedArray:");
        for(Node cur = dummaryNode.next;cur.next != null;cur = cur.next){
            buf.append(cur).append("->");
        }
        buf.append("Null");

        return buf.toString();
    }


    public static void main(String[] args){
        LinkedArray<Integer> linkedArray = new LinkedArray<>();
        for(int i = 0;i < 10;i ++){
            linkedArray.addLast(i);
            System.out.println(linkedArray);
        }
        linkedArray.set(66, 2);
        System.out.println(linkedArray);
        linkedArray.addFirst(8);
        System.out.println(linkedArray);
        linkedArray.removeLast();
        System.out.println(linkedArray);
        linkedArray.removeFirst();
        System.out.println(linkedArray);

    }

}
